import os
import msg
import urllib
import gzip
import MySQLdb

def runDownload(filename, world):
	url = 'http://en' + str(world) + '.grepolis.com/data/' + filename + '.txt.gz'
	msg.show('downloading ' + filename + '...')
	webFile = urllib.urlopen(url)
	localFile = open(url.split('/')[-1], 'w')
	localFile.write(webFile.read())
	webFile.close()
	localFile.close()
	msg.show(' DONE', 'green', 1, bold=True)

def runParse(filename):
	conn = MySQLdb.connect(host='localhost', user='root', passwd='', db='defcon')
	
	msg.show('\rparsing {0}... '.format(filename))
	
	f = gzip.open(filename + '.txt.gz')
	total = sum(1 for line in f)
	
	f = gzip.open(filename + '.txt.gz')
	conn.query('DELETE FROM {0}'.format(filename))
	for count, line in enumerate(f):
		msg.show('\rparsing {0}... '.format(filename))
		percentage = str(int(float(count) / float(total) * 100)) + '%'
		msg.show(percentage, 'green', bold=True)
		
		values = line.split(',')
		
		# Convert from timestamp to datetime
		if filename == 'conquers':
			values[1] = 'FROM_UNIXTIME(\'{0}\')'.format(values[1])
		
		# Add quotes
		for k, v in enumerate(values):
			if filename != 'conquers' or (filename == 'conquers' and k != 1):
				values[k] = '"' + v.replace('\n', '') + '"'
		
		sql = 'INSERT INTO {0} VALUES ({1});'.format(filename, ','.join(values))
		try:
			conn.query(sql)
		except MySQLdb.Error as (errnum, errstr):
			print ''
			msg.show('ERROR {0}:{1}\n{2}'.format(errnum, errstr, sql), 'red', 1)
			msg.show(str(line), 'red', 1)
			exit()
				
	conn.close()
	msg.show('\rparsing {0}... '.format(filename))
	msg.show('DONE', 'green', 1, bold=True)
				
	
def run(world='4', download=True, parse=True):	
	msg.show('-'*40, 'black', True)
	msg.show('Updating Data', 'yellow')
	msg.show(' (Press Ctrl+C to interrupt)', 'black', True)
	
	try:
		# Download
		if download:
			runDownload('players', world)
			runDownload('towns', world)
			runDownload('alliances', world)
			runDownload('islands', world) 
			runDownload('conquers', world)
		
		# Parse
		if parse:
			runParse('players')
			runParse('towns')
			runParse('alliances')
			runParse('islands')
			runParse('conquers')
	except KeyboardInterrupt:
		print ''
		msg.show('Interrupted', 'red', 1)
		return

if __name__ == '__main__':
	run('4', True, True);
